import * as dat from 'dat.gui';
import { remote } from 'electron';
import { editorCore, getEditorAPI, NodeInfo } from ".";
import * as fs from 'fs';
const { Menu, MenuItem } = remote;


export class Hierarchy {

    root: any;
    gui: any;
    currentNode: any;

    initView() {

        let currentNodeInfo;

        this.gui = new dat.GUI({ autoPlace: false });
        const customContainer = document.getElementById('hierarchy');
        customContainer.appendChild(this.gui.domElement);


        customContainer.addEventListener('contextmenu', (e) => {
            e.preventDefault();

            const menu = new Menu();
            menu.append(new MenuItem({
                label: '创建 GameObject', click: () => {
                    // console.log (1111)
                    if (currentNodeInfo) {
                        console.log(currentNodeInfo)
                        editorAPI.addGameObject(currentNodeInfo.uuId);
                    }
                    // editorAPI.addGameObject();
                    const saveData = editorAPI.save();
                    fs.writeFileSync(editorAPI.currentSceneFilePath, saveData, 'utf-8');
                }
            }));

            menu.append(new MenuItem({
                label: '删除 Gameobject', click: () => {
                    if (currentNodeInfo) {
                        console.log(currentNodeInfo)
                        editorAPI.removeGameObject(currentNodeInfo.uuId);
                    }
                    const saveData = editorAPI.save();
                    fs.writeFileSync(editorAPI.currentSceneFilePath, saveData, 'utf-8');
                }
            }));
            menu.popup({ window: remote.getCurrentWindow() });
            // if(isEleEditable(e.target)){
            //     menu.popup(remote.getCurrentWindow());
            // }else{
            //     //判断有文本选中
            //     let selectText = window.getSelection().toString();
            //     if(!!selectText){
            //         menu2.popup(remote.getCurrentWindow());
            //     }
            // }

        }, false)


        editorCore.addListenerOnSelectNode((node) => {


        })

        const editorAPI = getEditorAPI();
        let currentFolder = this.currentNode;
        editorAPI.addListener((info: NodeInfo) => {


            if (this.root) {
                this.gui.removeFolder(this.root);
            }
            this.root = this.gui.addFolder('root');
            this.root.open();

            function createGuiTree(parent, data: NodeInfo) {
                const folder = parent.addFolder(data.name);
                for (let child of data.children) {
                    createGuiTree(folder, child)
                }
                folder.domElement.onclick = (e: MouseEvent) => {
                    e.stopImmediatePropagation();
                    if (currentFolder) {
                        currentFolder.domElement.style.color = "#ffffff";
                    }
                    currentFolder = folder;
                    currentFolder.domElement.style.color = "yellow";
                    currentNodeInfo = data;
                    console.log(data.name);
                    editorCore.notifySelectNode(data)
                }
            }

            createGuiTree(this.root, info);


        })
    }
}